package com.yanqu.road.dao.impl.cross.banquet;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.cross.CrossBanquetRecord;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBanquetRecordDaoImpl extends TempDataObjectDao<CrossBanquetRecord> {

    public Map<String, CrossBanquetRecord> getBanquetRecordMap(long expireTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_c_banquet_record where openTime >= " + expireTime + " order by openTime DESC";
        Map<String, CrossBanquetRecord> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;

            rs = call.executeQuery();
            while (rs.next()) {
                CrossBanquetRecord data = parse(rs);
                res.put(data.getBanquetId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean updateData(CrossBanquetRecord data){
        return true;
    }

    @Override
    protected boolean addData(CrossBanquetRecord data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_banquet_record(banquetId,`type`,userId,record,openTime,overTime,memberCount) values(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getBanquetId());
            call.setInt(i++, data.getType());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getRecord());
            call.setLong(i++, data.getOpenTime());
            call.setLong(i++, data.getOverTime());
            call.setInt(i++, data.getMemberCount());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected CrossBanquetRecord parseData(ResultSet rs) throws SQLException {
        CrossBanquetRecord data = new CrossBanquetRecord();
        data.setBanquetId(rs.getString("banquetId"));
        data.setType(rs.getInt("type"));
        data.setUserId(rs.getLong("userId"));
        data.setRecord(rs.getString("record"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setOverTime(rs.getLong("overTime"));
        data.setMemberCount(rs.getInt("memberCount"));
        return data;
    }
}
